﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using MrPP.SerriedCabinet;
using MrPP.SocketSystem;

namespace MrPP.SerriedCabinet
{
    public class RespTheInitialStatus : GDGeek.Singleton<RespTheInitialStatus>, Resp
    {
      
    
        public static DateTime ConvertTimeStampToDateTime(long timeStamp)

        {

            DateTime defaultTime = new DateTime(1970, 1, 1, 0, 0, 0);

            long defaultTick = defaultTime.Ticks;

            long timeTick = defaultTick + timeStamp * 10000;

            //// 东八区 要加上8个小时

            DateTime dt = new DateTime(timeTick).AddHours(8);

            return dt;

        }
        void Start()
        {
            ProtoManager.Instance.addResp(this);
        }
        void OnDestroy()
        {
            if (ProtoManager.IsInitialized) { 
                ProtoManager.Instance.removeResp(this);
            }
        }
        public Action<Data> onRecevie
        {
            get;
            set;
        }
        [Serializable]
        public class Status
        {

            public byte area;
            public long datetime;
            public ushort[] positions;
            public ushort[] states;
        }
        [Serializable]
        public class Data {
            public List<Status> list = new List<Status>();
        }

        public static long GetTimeStamp(long time)
        {
            

            return (time - (new DateTime(1970, 1, 1, 16, 0, 0)).ToUniversalTime().Ticks) / 10000;

            
          
        }
        public Status createStatus(DataStream reader) {
            Status status = new Status();
            status.area = reader.ReadByte();
            status.datetime = reader.ReadSInt64();
            Debug.Log(ConvertTimeStampToDateTime(GetTimeStamp(status.datetime)).ToString());
            int n = GlobalConfigure.Instance.getCount(status.area);
            Debug.Log(reader.Length);
            Debug.Log(reader.Position);
            if (reader.Length - reader.Position == (n + 1) * 4)
            {
                short x = reader.ReadSInt16();
                status.positions = new ushort[x];   
                for (int i = 0; i < status.positions.Length; ++i)
                {
                    status.positions[i] = reader.ReadUInt16(true);
                }

                short y = reader.ReadSInt16();
                status.states = new ushort[y];
                for (int i = 0; i < status.states.Length; ++i)
                {
                    status.states[i] = reader.ReadUInt16(true);
                }
            }
            else {

                status.positions = new ushort[n - 1];
                for (int i = 0; i < status.positions.Length; ++i)
                {
                    status.positions[i] = reader.ReadByte();
                }  
                status.states = new ushort[n - 1];
                for (int i = 0; i < status.states.Length; ++i)
                {
                    status.states[i] = reader.ReadByte();
                }
            }         
         
            return status;


        }
        public void deserialize(DataStream reader)
        {
            ushort count = reader.ReadInt16();
            Data data = new Data();
            for (int i = 0; i < count; ++i) {
                data.list.Add(createStatus(reader));
            }


            Debug.Log("data" + JsonUtility.ToJson(data));
            onRecevie?.Invoke(data);
        }
        public short getCommand()
        {
            var bytes = new byte[] { 0xA0, 0x00 };
            return BitConverter.ToInt16(bytes, 0);
        }

        public void serialize(DataStream writer)
        {
            Debug.Log("writer");
        }
    }
}